//
//  AppDelegate.m
//  IMDome
//
//  Created by limaohuyu on 2020/8/12.
//  Copyright © 2020 czw. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseNavigationController.h"
#import "LoginViewController.h"
#import "BaseTabBarController.h"
#import "WebSocketRocketManger.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    if (kStringIsEmpty([APPUserDataManager shareInstance].token)) {
        [self userLogoutNotification];
    }else{
        [self userLoginNotification];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoginNotification) name:USER_LOGIN_SUCCESS_KEY object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogoutNotification) name:USER_LOGOUT_SUCCESS_KEY object:nil];

    [self.window makeKeyAndVisible];
    return YES;
}
//登录
- (void)userLoginNotification{
    [self createWebSocket];
    [UIView transitionWithView:[UIApplication sharedApplication].keyWindow duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    BOOL oldState = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
        self.window.rootViewController = [BaseTabBarController new];
    [UIView setAnimationsEnabled:oldState];
    } completion:^(BOOL finished) {
    }];
}
//退出
- (void)userLogoutNotification{
    [self eixtItemAction];
    BaseNavigationController *loginNav =   [[BaseNavigationController alloc]initWithRootViewController:[LoginViewController new]];
    self.window.rootViewController = loginNav;
}
//链接WebSocket
- (void)createWebSocket{
    [WebSocketRocketManger instance].senderId = [APPUserDataManager shareInstance].userId;
     [[WebSocketRocketManger instance] openWebSocketWithURLString:BASE_URL_WEBSOCKET];

}
//退出WebSocket
- (void)eixtItemAction{
    [[WebSocketRocketManger instance]closeWebSocket];
}
@end
